package com.cjkj.applet.util;

import com.alibaba.fastjson2.JSONObject;
import com.cjkj.applet.config.WechatConfig;
import com.cjkj.common.core.constant.EquipmentConstants;
import com.cjkj.system.api.RemoteWeChatService;
import com.cjkj.system.api.domain.dto.APIWechatSessionDto;
import com.cjkj.system.api.domain.request.APIWechatSessionReq;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 微信工具类
 */
@Component
public class WeChatUtil {

    public static WechatConfig wechatConfig;
    public static RemoteWeChatService remoteWeChatService;
    @Resource
    private WechatConfig wechatConfigTemp;
    @Resource
    private RemoteWeChatService remoteWeChatServiceTemp;
    @PostConstruct
    public void init(){
        wechatConfig = this.wechatConfigTemp;
        remoteWeChatService = this.remoteWeChatServiceTemp;
    }

    /**
     *
     * @param jsCode
     * @param dataType 0-睡眠垫监测小程序，1-多参数监测小程序
     * @return
     */
    public static APIWechatSessionDto getSession2Code(String jsCode, String dataType){
        APIWechatSessionReq req = new APIWechatSessionReq();
        req.setJsCode(jsCode);
        String appId = "";
        String appSecret = "";
        // 睡眠监测垫小程序
        if(EquipmentConstants.EQUIPMENT_TYPE_MONITOR_PAD_TYPE.equals(dataType)) {
            appId = wechatConfig.getAppId();
            appSecret = wechatConfig.getAppSecret();
        }
        // 多参数监测仪小程序
        if(EquipmentConstants.EQUIPMENT_TYPE_SMART_BRACELET_TYPE.equals(dataType)) {
            appId = wechatConfig.getBraceletAppId();
            appSecret = wechatConfig.getBraceletAppSecret();
        }
        // 测试版小程序
        if(EquipmentConstants.EQUIPMENT_TYPE_EB_TYPE.equals(dataType)) {
            appId = wechatConfig.getNewAppId();
            appSecret = wechatConfig.getNewAppSecret();
        }
        req.setAppid(appId);
        req.setSecret(appSecret);
        req.setGrantType("authorization_code");
        String sessionByCode = remoteWeChatService.getSessionByCode(req);
        return JSONObject.parseObject(sessionByCode, APIWechatSessionDto.class);
    }
}